package com.bds.wm.action;

import com.alibaba.fastjson.JSONObject;
import com.bds.wm.model.common.Result;
import com.bds.wm.model.procmodel.*;
import com.bds.wm.service.OverviewService;
import com.bds.wm.utils.CheckParams;
import com.bds.wm.utils.Const;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * Created by guoyu on 2018/5/16.
 */
@Controller
public class OverviewAction extends BaseAction {
    private static Logger LOGGER = Logger.getLogger(OverviewAction.class);

    @Resource
    private OverviewService overviewService;

    @RequestMapping(value = "/service/data/get_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                    String start_month, String end_month, String city_tier) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        OverviewResult overviewResult = overviewService.getOverviewResult(startMonth, endMonth, city_tier);
        result.setResult(Const.SUC, overviewResult);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_rate", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewRateResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                        String start_month, String end_month, String city_tier) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<OverviewResultYoyMom> overviewResultRate = overviewService.getOverviewRateResult(startMonth, endMonth, city_tier);
        result.setResult(Const.SUC, overviewResultRate);

        return result;
    }

    @RequestMapping(value = "/service/data/get_brand_overview_result_rate", method = RequestMethod.GET)
    @ResponseBody
    public Result getBrandOverviewResultRate(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                             String start_month, String end_month, String brand_name, String subcat_name) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        try {
            brand_name = URLDecoder.decode(brand_name, "utf-8");
            subcat_name = URLDecoder.decode(subcat_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        List<OverviewResultYoyMom> overviewResultRate = overviewService.getOverviewRateResult(startMonth, endMonth, brand_name);
        result.setResult(Const.SUC, overviewResultRate.get(0));
        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_by_subcategory", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultBySubCategory(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                                 String start_month, String end_month, String city_tier, String category_tier) {
        Result result = new Result();
        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        JSONObject jsonObject = overviewService.getOverviewResultBySubCategory(startMonth, endMonth, city_tier, category_tier);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_by_subcategory_trend", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultBySubCategoryTrend(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                                      String start_month, String end_month, String city_tier,
                                                      String category_tier, String category_name) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Integer categoryTier = CheckParams.checkIfIntAndReturn(category_tier);
        try {
            category_name = URLDecoder.decode(category_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null || categoryTier == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<OverviewResultBySubCategoryTrend> overviewResultBySubCategoryTrendList = overviewService
                .getOverviewResultBySubCategoryTrend(startMonth, endMonth, city_tier, categoryTier, category_name);
        result.setResult(Const.SUC, overviewResultBySubCategoryTrendList);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_by_city", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultByCity(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                          String start_month, String end_month, String city_tier) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        JSONObject jsonObject = overviewService.getOverviewResultByCity(startMonth, endMonth, city_tier);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_by_indexchannel", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultByChannel(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                             String start_month, String end_month, String city_tier) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        JSONObject jsonObject = overviewService.getOverviewResultByChannel(startMonth, endMonth, city_tier);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }

    @RequestMapping(value = "/service/data/get_monthly_result_by_channel", method = RequestMethod.GET)
    @ResponseBody
    public Result getMonthlyResultBychannel(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                            String start_month, String end_month, String city_tier, String month_num) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Integer monthNum = CheckParams.checkIfIntAndReturn(month_num);
        if (startMonth == null || endMonth == null || monthNum == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        JSONObject jsonObject = overviewService.getMonthlyResultBychannel(startMonth, endMonth, city_tier, monthNum);
        result.setResult(Const.SUC, jsonObject);

        return result;
    }


    @RequestMapping(value = "/service/data/get_top_brand_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getTopBrandOverviewResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                            String start_month, String end_month, String city_name,
                                            String category_name, String city_tier) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        String cityName = "", categoryName = "";
        try {
            cityName = URLDecoder.decode(city_name, "utf-8");
            categoryName = URLDecoder.decode(category_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<TopBrandOverviewResult> list = overviewService
                .getTopBrandOverviewResult(startMonth, endMonth, cityName, categoryName, city_tier);
        result.setResult(Const.SUC, list);

        return result;
    }

    @RequestMapping(value = "/service/data/get_map_overview_result", method = RequestMethod.GET)
    @ResponseBody
    public Result getMapOverviewResult(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                       String start_month, String end_month, String brand_name,
                                       String max_lng, String min_lng, String max_lat, String min_lat) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Double maxLng = CheckParams.checkIfDoubleAndReturn(max_lng);
        Double minLng = CheckParams.checkIfDoubleAndReturn(min_lng);
        Double maxLat = CheckParams.checkIfDoubleAndReturn(max_lat);
        Double minLat = CheckParams.checkIfDoubleAndReturn(min_lat);
        String brandName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null
                || maxLng == null || minLng == null || maxLat == null || minLat == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<MapOverviewResult> list = overviewService
                .getMapOverviewResult(startMonth, endMonth, brandName, maxLng, minLng, maxLat, minLat);
        result.setResult(Const.SUC, list);

        return result;
    }

    @RequestMapping(value = "/service/data/get_map_overview_result_by_capacity", method = RequestMethod.GET)
    @ResponseBody
    public Result getMapOverviewResultByCapacity(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                                 String start_month, String end_month, String brand_name,
                                                 String lng, String lat) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        Float realLng = CheckParams.checkIfFloatAndReturn(lng);
        Float realLat = CheckParams.checkIfFloatAndReturn(lat);
        String brandName = "";
        try {
            brandName = URLDecoder.decode(brand_name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null || realLng == null || realLat == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<MapOverviewCapacity> list = overviewService
                .getMapOverviewResultByCapacity(startMonth, endMonth, brandName, realLng, realLat);
        result.setResult(Const.SUC, list);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_rate_of_sub", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultRateOfSub(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                             String start_month, String end_month,
                                             String city_tier, String sub_category) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        String cityTier = "", subCategory = "";
        try {
            cityTier = URLDecoder.decode(city_tier, "utf-8");
            subCategory = URLDecoder.decode(sub_category, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        OverviewResultRateOfSub overviewResultRateOfSub = overviewService.getOverviewResultRateOfSub(startMonth, endMonth,
                cityTier, subCategory);
        result.setResult(Const.SUC, overviewResultRateOfSub);

        return result;
    }

    @RequestMapping(value = "/service/data/get_overview_result_of_sub", method = RequestMethod.GET)
    @ResponseBody
    public Result getOverviewResultOfSub(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                         String start_month, String end_month,
                                         String city_tier, String sub_category) {
        Result result = new Result();

        Integer startMonth = CheckParams.checkIfIntAndReturn(start_month);
        Integer endMonth = CheckParams.checkIfIntAndReturn(end_month);
        String cityTier = "", subCategory = "";
        try {
            cityTier = URLDecoder.decode(city_tier, "utf-8");
            subCategory = URLDecoder.decode(sub_category, "utf-8");
        } catch (UnsupportedEncodingException e) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }
        if (startMonth == null || endMonth == null) {
            LOGGER.info("参数异常");
            result.setResult(Const.FAIL, "参数异常");
            return result;
        }

        List<OverviewResultOfSub> overviewResultOfSubList = overviewService.getOverviewResultOfSub(startMonth, endMonth,
                cityTier, subCategory);
        result.setResult(Const.SUC, overviewResultOfSubList);

        return result;
    }

}
